package cn.edu.yzu.scholarship.controller;

import cn.edu.yzu.scholarship.constant.C;
import cn.edu.yzu.scholarship.kit.Sak;
import cn.edu.yzu.scholarship.model.College;
import cn.edu.yzu.scholarship.validator.ScholarActValidator;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Db;

/**
 * 学校课程、专业、学院、奖学金等基本信息的查询
 * @author francis
 *
 */
public class UniCtrler extends Controller {
	
	private static final String MAJOR_FIND_ALL = "major.findAll";
	private static final String COLLEGE_FIND_ALL = "college.findAll";
	
	private static final String SCHOLARSHIP_FIND_ALL = "scholarship.findAll";
	private static final String SCHOLARSHIP_FIND_USEABLE = "scholarship.findUsable";
	private static final String SCHOLARSHIP_FIND_LEVEL = "scholarship.findLevel";
	private static final String SCHOLARSHIP_FIND_MAINBYNAME = "scholarship.findMainByName";
	private static final String SCHOLARSHIP_INSERT_MAIN = "scholarship.insertMain";
	private static final String SCHOLARSHIP_UPDATE_MAIN = "scholarship.updateMain";
	private static final String SCHOLARSHIP_DELETE_MAIN = "scholarship.deleteMain";
	private static final String SCHOLARSHIP_DELETE_LEVELS = "scholarship.deleteLevels";
	private static final String SCHOLARSHIP_INSERT_LEVEL = "scholarship.insertLevel";
	
	public void colleges() {
		renderJson(Db.find(SqlKit.sql(COLLEGE_FIND_ALL)));
	}
	
	public void majors() {
		String college = getPara("college", "-");//默认全选
		String sqlSuffix = C.PARAM_ALL.equals(college) ? "" : " where lqyxsm in (" + college + ")";
		renderJson(Db.find(SqlKit.sql(MAJOR_FIND_ALL) + sqlSuffix));
	}
	
	public void scholars(){
		renderJson(Db.find(SqlKit.sql(SCHOLARSHIP_FIND_ALL)));
	}
	
	public void useableScholars(){
		renderJson(Db.find(SqlKit.sql(SCHOLARSHIP_FIND_USEABLE)));
	}
	
	public void scholarLevels() {
		renderJson(Db.find(SqlKit.sql(SCHOLARSHIP_FIND_LEVEL),getParaToInt(0)));
	}

	@Before(ScholarActValidator.class)
	public void scholarAct() {
		String action = getPara("action");
		String name = getPara("name");
		String startMonth = getPara("startMonth");
		String endMonth = getPara("endMonth");
		int frequency = getParaToInt("frequency",-1);
		int level = getParaToInt("level",-1);
		
		if("edit".equals(action)) editScholar(name, level, startMonth, endMonth, frequency, getPara("scholarId"));
		else if("add".equals(action)) addScholar(name, level, startMonth, endMonth, frequency);
		else if("del".equals(action)) delScholar(getPara("scholarId"));
		renderText(Sak.toJsonStr("errorInfo", "none"));//通知页面，处理过程没有问题。
		//renderJson("errorInfo","none");//IE form submit bug
	}

	private void delScholar(String scholarId) {
		Db.update(SqlKit.sql(SCHOLARSHIP_DELETE_MAIN), scholarId);
		Db.update(SqlKit.sql(SCHOLARSHIP_DELETE_LEVELS), scholarId);
	}

	private void addScholar(String name, int level,String startMonth, String endMonth, int frequency) {
		Db.update(SqlKit.sql(SCHOLARSHIP_INSERT_MAIN), name, level, startMonth, endMonth, frequency);
		Object scholarId = Db.findFirst(SqlKit.sql(SCHOLARSHIP_FIND_MAINBYNAME), name).get("id");
		for(int i = 1; i <= level; i++) {
			Db.update(SqlKit.sql(SCHOLARSHIP_INSERT_LEVEL), scholarId, i, getPara("name" + i),getPara("money" + i));
		}
	}

	private void editScholar(String name, int level, String startMonth, String endMonth, int frequency, String scholarId) {
		Db.update(SqlKit.sql(SCHOLARSHIP_UPDATE_MAIN), name, level, startMonth, endMonth, frequency, scholarId);
		Db.update(SqlKit.sql(SCHOLARSHIP_DELETE_LEVELS), scholarId);
		for(int i = 1; i <= level; i++) {
			Db.update(SqlKit.sql(SCHOLARSHIP_INSERT_LEVEL), scholarId, i, getPara("name" + i),getPara("money" + i));
		}
	}
	
	public void mergeCollege() {
		College.mergeCollege(getPara("oldIds"), getPara("newCollegeName"));
		renderJson("errorInfo","none");
	}
	
	public void nextCollegeId() {
		renderJson(Db.findFirst(SqlKit.sql("college.findNextLqyxsm")));
	}
	
	public void collegeAct() {
		String action = getPara("action");
		
		String collegeName = getPara("collegeName");
		String collegeId = getPara("collegeId");
		String newCollegeId = getPara("newCollegeId",collegeId);
		
		if("add".equals(action)) addCollege(newCollegeId,collegeName);
		else if("del".equals(action)) delCollege(collegeId);
		else if("edit".equals(action)) editCollege(collegeId,collegeName);
		
		renderJson("errorInfo","none");
	}

	private void editCollege(String collegeId, String collegeName) {
		Db.update(SqlKit.sql("college.update"), collegeName, collegeId);
		Db.update(SqlKit.sql("grant.mergeCollege"), collegeId, collegeName, collegeId);
		Db.update(SqlKit.sql("student.mergeCollege"), collegeId, collegeName, collegeId);
		Db.update(SqlKit.sql("major.mergeCollege"), collegeId, collegeName, collegeId);
	}

	private void delCollege(String collegeId) {
		for(String id : collegeId.split(","))
			Db.update(SqlKit.sql("college.delete"), id);
	}

	private void addCollege(String collegeId, String collegeName) {
		Db.update(SqlKit.sql("college.insert"), collegeId, collegeName);
	}
}
